﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NCS.Library.Components.Security;
using NCS.Library;
using NCS.Library.Utilities;
using NCS.Module.CMS.Library.Definitions;
using NCS.Module.CMS.Library.CMSPermisions;
using System.IO;
using NCS.Module.Multimedia.Library.Bonus;
namespace NCS.Module.Multimedia.Admin.Bonus
{
    public partial class CateBonusManager : AuthenticatedControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            isManager = CMSPermisionController.CheckUserPermision(CurrentUser.UserID, (int)CMSPermision.Manager, ConvertUtility.ToInt32(AppEnv.PortalId()));
            isPublisher = CMSPermisionController.CheckUserPermision(CurrentUser.UserID, (int)CMSPermision.Publisher, ConvertUtility.ToInt32(AppEnv.PortalId()));
        }
        private bool isManager, isPublisher;
        private string userUploadPath = Library.Definies.UploadPath;
        private void CheckPath()
        {
            userUploadPath += CurrentUser.UserID + "/Nam_" + DateTime.Now.Year.ToString();
            string folder = userUploadPath + "/Thang_" + DateTime.Now.Month.ToString() + "/Ngay_" + DateTime.Now.Day.ToString();
            string avatarPath = folder + "/Avatar/";

            if (!Directory.Exists(Server.MapPath(folder)))
            {
                Directory.CreateDirectory(Server.MapPath(folder));
            }
            if (!Directory.Exists(Server.MapPath(avatarPath)))
            {
                Directory.CreateDirectory(Server.MapPath(avatarPath));
            }
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            DataTable dtRebuildData = new DataTable();

            dtRebuildData.Columns.Add("CateBonusID", typeof(String));
            dtRebuildData.Columns.Add("CateBonusParentID", typeof(String));
            dtRebuildData.Columns.Add("CateBonusAvatar", typeof(String));
            dtRebuildData.Columns.Add("CateBonusName", typeof(String));
            dtRebuildData.Columns.Add("CateBonusDetail", typeof(String));
            dtRebuildData.Columns.Add("Visible", typeof(String));
            dtRebuildData.Columns.Add("Indexs", typeof(String));
            dtRebuildData.Columns.Add("Rank", typeof(String));
            dtRebuildData.Columns.Add("Level", typeof(String));

            CheckPath();
            txtAvatar.fpUploadDir = userUploadPath;

            DataTable dtCateBonus = Gallery_CateBonusController.GetCateBonusByParentID(Gallery_CateBonusController.GetAllCateBonus(), dtRebuildData);

            dtgCateBonusList.DataSource = dtCateBonus;
            dtgCateBonusList.DataBind();

            dropCateBonus.DataSource = dtCateBonus;
            dropCateBonus.DataBind();

            dropCateBonus.Items.Insert(0, new ListItem("- Chọn Album Bonus -", "0"));

            int catebonusid = ConvertUtility.ToInt32(Request.QueryString["catebonusid"]);
            Gallery_CateBonusInfo info = Gallery_CateBonusController.GetCateBonus(catebonusid);
            if (info != null)
            {
                btnUpdate.Visible = true;
                //btnAdd.Visible = false;
                txtCateBonusName.Text = info.CateBonusName;
                if (info.CateBonusAvatar != "")
                {
                    txtAvatar.fpUploadDir = info.CateBonusAvatar.Substring(0, info.CateBonusAvatar.LastIndexOf("/"));
                }
                txtAvatar.Text = info.CateBonusAvatar;
                Editor1.HTMLValue = info.CateBonusDetail;
                chkalbum.Checked = info.Visible;
                chkcomment.Checked = info.IsComment;
                dropCateBonus.SelectedValue = info.CateBonusParentID.ToString();
                rdtype.SelectedValue = info.Rank.ToString();
                tb_add.Visible = true;
            }
            else
            {
                //btnAdd.Visible = true;
                btnUpdate.Visible = false;
            }
        }

        protected void dtgCateBonusList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex < 0) return;
            DataRowView currow = (DataRowView)e.Row.DataItem;
            HyperLink lnkHeadline = (HyperLink)e.Row.FindControl("lnkHeadline");
            DropDownList dropIndex = (DropDownList)e.Row.FindControl("dropIndex");
            DropDownList dropRank = (DropDownList)e.Row.FindControl("dropRank");
            DropDownList dropStatus = (DropDownList)e.Row.FindControl("dropStatus");

            lnkHeadline.Text = currow["CateBonusName"].ToString();
            lnkHeadline.ToolTip = "Xem chi tiết Album này";
            lnkHeadline.NavigateUrl = AppEnv.AdminUrlParams("picturelist") + "&catebonusid=" + currow["CateBonusID"].ToString();
            dropRank.SelectedValue = currow["Rank"].ToString();
            dropStatus.SelectedValue = ConvertUtility.ToBoolean(currow["Visible"]) == true ? "1" : "0";
            MiscUtility.FillIndex(dropIndex, 40, ConvertUtility.ToInt32(currow["Indexs"]));
        }

        protected void dtgCateBonusList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int catebonusid = ConvertUtility.ToInt32(e.CommandArgument);
            try
            {
                if (e.CommandName == "edit")
                {
                    Response.Redirect(AppEnv.AdminUrlParams("catebonuslist") + "&catebonusid=" + catebonusid);
                }
                else if (e.CommandName == "updatepriority")
                {
                    if (isManager || isPublisher)
                    {
                        foreach (GridViewRow item in dtgCateBonusList.Rows)
                        {
                            DropDownList dropIndex = (DropDownList)item.FindControl("dropIndex");
                            Gallery_CateBonusController.SetCateBonusIndex(ConvertUtility.ToInt32(item.Cells[0].Text), ConvertUtility.ToInt32(dropIndex.SelectedValue));
                        }
                    }
                }
                else if (e.CommandName == "updaterank")
                {
                    if (isManager || isPublisher)
                    {
                        foreach (GridViewRow item in dtgCateBonusList.Rows)
                        {
                            DropDownList dropRank = (DropDownList)item.FindControl("dropRank");
                            Gallery_CateBonusController.SetCateBonusRank(ConvertUtility.ToInt32(item.Cells[0].Text), ConvertUtility.ToInt32(dropRank.SelectedValue));
                        }
                    }
                }
                else if (e.CommandName == "updatestatus")
                {
                    if (isManager || isPublisher)
                    {
                        foreach (GridViewRow item in dtgCateBonusList.Rows)
                        {
                            DropDownList dropStatus = (DropDownList)item.FindControl("dropStatus");
                            Gallery_CateBonusController.SetCateBonusStatus(ConvertUtility.ToInt32(item.Cells[0].Text), dropStatus.SelectedValue == "1" ? true : false);
                        }
                    }
                }
                else if (e.CommandName == "delete")
                {
                    Gallery_CateBonusController.DeleteCateBonus(catebonusid);
                }

                lbupdate.Text = MiscUtility.MSG_UPDATE_SUCCESS;
            }
            catch
            {
                lbupdate.Text = MiscUtility.MSG_UPDATE_ERROR;
            }
        }

        protected void reset()
        {
            txtCateBonusName.Text = "";
            txtAvatar.Text = "";
            Editor1.HTMLValue = "";
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                Gallery_CateBonusInfo info = new Gallery_CateBonusInfo();
                info.CateBonusAvatar = txtAvatar.Text;
                MultimediaUtility.SetAvatarGalleryThumbnail(txtAvatar.Text, NCS.Module.Multimedia.Library.Definies.ThumbWidth, NCS.Module.Multimedia.Library.Definies.ThumbHeight);
                info.CateBonusDetail = Editor1.HTMLValue;
                info.CateBonusName = txtCateBonusName.Text;
                info.CateBonusParentID = ConvertUtility.ToInt32(dropCateBonus.SelectedValue);
                info.CreateDate = DateTime.Now;
                info.ModifiedDate = DateTime.Now;
                info.ModifyUserID = CurrentUser.UserID;
                info.PortalID = ConvertUtility.ToInt32(AppEnv.PortalId());
                info.Visible = chkalbum.Checked;
                info.IsComment = chkcomment.Checked;
                info.Lang = AppEnv.GetLanguage();
                info.Rank = ConvertUtility.ToInt32(rdtype.SelectedValue);
                int catebonusid = Gallery_CateBonusController.InsertCatebonus(info);
                reset();
                if (catebonusid > 0)
                    lbupdate.Text = MiscUtility.MSG_UPDATE_SUCCESS;
                tb_add.Visible = false;
                tableGrid.Visible = true;
            }
            catch
            {
                lbupdate.Text = MiscUtility.MSG_UPDATE_ERROR;
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                int catebonusid = ConvertUtility.ToInt32(Request.QueryString["catebonusid"]);
                if (catebonusid > 0)
                {
                    Gallery_CateBonusInfo info = Gallery_CateBonusController.GetCateBonus(catebonusid);
                    info.CateBonusID = catebonusid;
                    info.CateBonusAvatar = txtAvatar.Text;
                    MultimediaUtility.SetAvatarGalleryThumbnail(txtAvatar.Text, NCS.Module.Multimedia.Library.Definies.ThumbWidth, NCS.Module.Multimedia.Library.Definies.ThumbHeight);
                    info.CateBonusDetail = Editor1.HTMLValue;
                    info.CateBonusParentID = ConvertUtility.ToInt32(dropCateBonus.SelectedValue);
                    info.CateBonusName = txtCateBonusName.Text;
                    info.ModifiedDate = DateTime.Now;
                    info.ModifyUserID = CurrentUser.UserID;
                    info.IsComment = chkcomment.Checked;
                    info.Rank = ConvertUtility.ToInt32(rdtype.SelectedValue);
                    info.Visible = chkalbum.Checked;
                    Gallery_CateBonusController.UpdateAlbum(info);
                    lbupdate.Text = MiscUtility.MSG_UPDATE_SUCCESS;

                    tb_add.Visible = false;
                    tableGrid.Visible = true;
                    //btnAdd.Visible = true;
                    //btnUpdate.Visible = false;
                }
            }
            catch
            {
                lbupdate.Text = MiscUtility.MSG_UPDATE_ERROR;
            }
        }

        protected void btnthemmoi_Click(object sender, EventArgs e)
        {
            tb_add.Visible = true;
            tableGrid.Visible = false;
        }

        protected void dtgCateBonusList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void dtgCateBonusList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }
}